package com.yt.setclaim.server.api.cms;

import com.yt.common.constant.Result;
import com.yt.setclaim.models.entity.ClaimClose;
import com.yt.setclaim.models.service.ClaimCloseService;
import com.yt.setclaim.models.vo.ClaimCloseVO;
import com.yt.setclaim.models.vo.FinancialPayVO;
import com.yt.setclaim.models.dto.ClaimCloseDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cms/setclaim-close")
public class ClaimCloseController {

    @Autowired
    private ClaimCloseService claimCloseService;

    /**
     * 获取所有理赔结案记录
     */
    @GetMapping("/list")
    public Result<List<ClaimCloseVO>> list() {
        List<ClaimClose> list = claimCloseService.list();
        List<ClaimCloseVO> voList = list.stream().map(claimClose -> {
            ClaimCloseVO vo = new ClaimCloseVO();
            BeanUtils.copyProperties(claimClose, vo);
            // ID转换为字符串类型
            vo.setCloseId(String.valueOf(claimClose.getCloseId()));
            vo.setPayId(String.valueOf(claimClose.getPayId()));
            vo.setAdminId(String.valueOf(claimClose.getAdminId()));
            return vo;
        }).collect(Collectors.toList());
        return Result.success(voList);
    }
    
    /**
     * 获取待结案的财务支付记录
     */
    @GetMapping("/pending-list")
    public Result<List<FinancialPayVO>> getPendingPayments() {
        return Result.success(claimCloseService.getPendingPayments());
    }

    /**
     * 根据ID获取理赔结案记录
     */
    @GetMapping("/info/{id}")
    public Result<ClaimCloseVO> info(@PathVariable("id") Integer id) {
        ClaimClose claimClose = claimCloseService.getById(id);
        ClaimCloseVO vo = new ClaimCloseVO();
        BeanUtils.copyProperties(claimClose, vo);
        // ID转换为字符串类型
        vo.setCloseId(String.valueOf(claimClose.getCloseId()));
        vo.setPayId(String.valueOf(claimClose.getPayId()));
        vo.setAdminId(String.valueOf(claimClose.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 根据支付ID获取理赔结案记录
     */
    @GetMapping("/info-by-pay/{payId}")
    public Result<ClaimCloseVO> infoByPayId(@PathVariable("payId") Integer payId) {
        ClaimClose claimClose = claimCloseService.getByPayId(payId);
        if (claimClose == null) {
            return Result.fail("未找到对应的理赔结案记录");
        }
        ClaimCloseVO vo = new ClaimCloseVO();
        BeanUtils.copyProperties(claimClose, vo);
        // ID转换为字符串类型
        vo.setCloseId(String.valueOf(claimClose.getCloseId()));
        vo.setPayId(String.valueOf(claimClose.getPayId()));
        vo.setAdminId(String.valueOf(claimClose.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 添加理赔结案记录
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody ClaimCloseDTO claimCloseDTO) {
        ClaimClose claimClose = new ClaimClose();
        BeanUtils.copyProperties(claimCloseDTO, claimClose);
        // ID转换为Integer类型
        if (claimCloseDTO.getCloseId() != null) {
            claimClose.setCloseId(Integer.valueOf(claimCloseDTO.getCloseId()));
        }
        if (claimCloseDTO.getPayId() != null) {
            claimClose.setPayId(Integer.valueOf(claimCloseDTO.getPayId()));
        }
        if (claimCloseDTO.getAdminId() != null) {
            claimClose.setAdminId(Integer.valueOf(claimCloseDTO.getAdminId()));
        }
        claimCloseService.save(claimClose);
        return Result.success("添加成功");
    }

    /**
     * 修改理赔结案记录
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody ClaimCloseDTO claimCloseDTO) {
        ClaimClose claimClose = new ClaimClose();
        BeanUtils.copyProperties(claimCloseDTO, claimClose);
        // ID转换为Integer类型
        if (claimCloseDTO.getCloseId() != null) {
            claimClose.setCloseId(Integer.valueOf(claimCloseDTO.getCloseId()));
        }
        if (claimCloseDTO.getPayId() != null) {
            claimClose.setPayId(Integer.valueOf(claimCloseDTO.getPayId()));
        }
        if (claimCloseDTO.getAdminId() != null) {
            claimClose.setAdminId(Integer.valueOf(claimCloseDTO.getAdminId()));
        }
        claimCloseService.updateById(claimClose);
        return Result.success("修改成功");
    }

    /**
     * 删除理赔结案记录
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody ClaimCloseDTO claimCloseDTO) {
        claimCloseService.removeById(Integer.valueOf(claimCloseDTO.getCloseId()));
        return Result.success("删除成功");
    }
}